package list.test;

import java.io.*;
import java.util.ArrayList;

/**
 * @author PC
 * @date 2020/5/18
 */
public class SerializableTest2 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //创建四个学生对象
        Student s1 = new Student("张三",23);
        Student s2 = new Student("李四",24);
        Student s3 = new Student("王五",25);
        Student s4 = new Student("赵六",26);
        //创建集合存储学生对象
        ArrayList<Student> list = new ArrayList<>();
        //添加元素
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        //序列化
        //创建对象输出流
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("collection/src/list/test/obj.txt"));
        oos.writeObject(list);
        oos.close();
        //反序列化
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("collection/src/list/test/obj.txt"));
        ArrayList<Student> lists = (ArrayList<Student>) ois.readObject();
        for (Student student : lists) {
            System.out.println(student);
        }
    }
}
